<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item to="/home/product/spumanage"
        >商品管理</el-breadcrumb-item
      >
      <el-breadcrumb-item>属性模板管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div class="box">
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <h3 style="line-height: 32px">
              <i class="el-icon-s-tools"></i> &nbsp; 管理信息
            </h3>
          </div>
        </el-col>
        <el-col :span="16" style="text-align: right">
          <div class="grid-content bg-purple">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="addAttrDialogVisible = true"
              >添加属性模板</el-button
            >
            <el-button size="small" type="primary" icon="el-icon-remove-outline"
              >删除属性模板</el-button
            >
          </div>
        </el-col>
      </el-row>
      <div class="divider"></div>

      <el-row :gutter="15" class="whererow">
        <el-col :span="4" style="line-height: 32px">当前共{{total}}个模板。</el-col>
        <el-col :span="6">
          <el-input
            v-model="search"
            type="text"
            size="small"
            placeholder="请输入模板名称"
          >
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </el-col>
      </el-row>
    </div>

    <div class="noticerow box">
      <i class="el-icon-document"></i> &nbsp; 属性模板列表
    </div>

    <el-table
      border
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column align="center" label="编号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" label="模板名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <!-- <el-table-column align="center" label="属性数量" width="100">
                <template slot-scope="scope">{{ scope.row.attrCount }}</template>
            </el-table-column>
            <el-table-column align="center" label="参数数量" width="100">
                <template slot-scope="scope">{{ scope.row.paramCount }}</template>
            </el-table-column> -->
      <el-table-column align="center" label="设置" width="300">
        <template slot-scope="scope">
          <el-button
            title="非销售属性列表"
            size="mini"
            icon="el-icon-tickets"
            @click="
              $router.push(
                `/home/product/nosalesattributemanage/${scope.row.id}`
              )
            "
            >非销售属性列表</el-button
          >
          <el-button
            title="销售属性列表"
            size="mini"
            icon="el-icon-document-copy"
            @click="
              $router.push(
                `/home/product/salesattributemanage/${scope.row.id}`
              )
            "
            >销售属性列表</el-button
          >
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="170">
        <template slot-scope="scope" type="index">
          <el-button
            title="编辑属性模板"
            type="warning"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="showUpdateTemplateDialog(scope.$index)"
          ></el-button>
          <el-button
            title="删除属性模板"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="deleteAttrTemplate(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        layout="total, prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="添加属性模版"
      :visible.sync="addAttrDialogVisible"
      :modal-append-to-body="false"
    >
      <el-form :model="addForm">
        <el-form-item label="请选择分类" prop="categoryId" label-width="120px">
          <el-cascader
            ref="cascader"
            filterable
            placeholder="试试 搜索关键字"
            :props="props"
            v-model="addForm.categoryId"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="属性模版名称" prop="name" label-width="120px">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性模版拼音" prop="pinyin" label-width="120px">
          <el-input v-model="addForm.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="属性模版关键词"
          prop="keywords"
          label-width="120px"
        >
          <el-input v-model="addForm.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性模版序号" prop="sort" label-width="120px">
          <el-input v-model="addForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addAttrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddAttrForm()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="修改属性模版"
      :visible.sync="updateAttrDialogVisible"
      :modal-append-to-body="false"
    >
      <el-form :model="updateForm">
        <el-form-item label="请选择分类" prop="categoryId" label-width="120px">
          <el-cascader
            ref="cascader"
            filterable
            placeholder="点击修改默认类别"
            :props="props"
            v-model="updateForm.categoryId"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="属性模版名称" prop="name" label-width="120px">
          <el-input v-model="updateForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性模版拼音" prop="pinyin" label-width="120px">
          <el-input v-model="updateForm.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="属性模版关键词"
          prop="keywords"
          label-width="120px"
        >
          <el-input v-model="updateForm.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性模版序号" prop="sort" label-width="120px">
          <el-input v-model="updateForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateAttrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateAttrForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addAttrDialogVisible: false,                // 绑定添加属性模板弹窗是否显示
      updateAttrDialogVisible: false,             // 绑定修改属性模板弹窗是否显示
      selectedTemplateId: 0,                      // 当前选中模板ID
      addForm: {},                                // 绑定添加属性模板表单
      updateForm: {},                             // 绑定修改属性模板表单
      currentPage: 1,                             // 当前页 
      total: 0,                                   // 总页数
      pageSize: 5,                                // 每页条目数
      search: "",                                 // 绑定搜索文本框的值
      tableData: [],                              // 绑定表格数据
      props: {
        lazy: true, // 懒加载
        emitPath: false, // 设置 false，只返回该节点的值
        lazyLoad: (node, resolve) => {
          /** 定义懒加载方式 */
          console.log("选择的当前节点：", node);
          let parentId = 0;
          if (!node.root) {
            parentId = node.data.value;
          }
          this.$api.categoryApi
            .queryByParent({ parentId: parentId })
            .then((res) => {
              console.log("加载子类别：", res);
              if(res.data.state==200){
                // 整理数据结构, 返回类别数组 [{label:'类别名', value:'类别id'}...]
                let nodes = [];
                res.data.data.forEach((item) => {
                  nodes.push({
                    label: item.name,
                    value: item.id,
                    leaf: item.parent != 1,
                  });
                });
                resolve(nodes);
              }else{
                this.$alert(res.data.message, '提示')
              }
            });
        },
      },
    };
  },
  methods: {
    /**
     *  显示修改模板弹出窗口 
     */
    showUpdateTemplateDialog(index) {
      console.log(index);
      // 将选中项的模板ID，存入data.selectedTemplateId 供提交表单所用
      this.updateForm = this.tableData[index];
      this.updateAttrDialogVisible = true;
    },

    /**
     *  删除所选中的属性模板 
     **/
    deleteAttrTemplate(id) {
      // 弹出确认对话框
      this.$confirm("确认删除此属性模板？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.attrTemplateApi.delete({ id }).then((res) => {
          console.log("删除选中的属性模板：", res);
          if (res.data.state == 200) {
            this.$message({
              showClose: true,
              message: "删除属性模板成功",
              type: "success",
            });
            // 刷新属性模板列表
            this.loadAttrtempList();
          }else{
            this.$alert(res.data.message, '提示')
          }
        });
      });
    },

    /**
     *  提交添加属性模版的表单 
     */
    submitAddAttrForm() {
      this.$api.attrTemplateApi.add(this.addForm).then((res) => {
        console.log("添加属性模板：", res);
        if (res.data.state == 200) {
          // 添加成功，给出提示，让弹窗消失
          this.$message({
            showClose: true,
            message: "恭喜你，新增属性模板成功",
            type: "success",
          });
          this.addAttrDialogVisible = false;
          // 刷新属性模板列表
          this.loadAttrtempList();
        }
      });
    },

    /**
     *  提交修改属性模版的表单 
     */
    submitUpdateAttrForm() {
      this.$api.attrTemplateApi.update(this.updateForm).then((res) => {
        console.log("修改属性模板：", res);
        if (res.data.state == 200) {
          // 添加成功，给出提示，让弹窗消失
          this.$message({
            showClose: true,
            message: "恭喜你，修改属性模板成功",
            type: "success",
          });
          this.updateAttrDialogVisible = false;
          // 刷新属性模板列表
          this.loadAttrtempList();
        }else{
          this.$alert(res.data.message, '提示')
        }
      });
    },

    /**
     *  加载所有属性模板列表 
     */
    loadAttrtempList() {
      let page = this.currentPage;
      let pageSize = this.pageSize;
      this.$api.attrTemplateApi.queryAll({ page, pageSize }).then((res) => {
        console.log("加载所有属性模板对象:", res);
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      });
    },

    /**
     *  处理更新页码后的业务 
     */
    handleCurrentChange(val) {
      console.log(`即将加载选中页: 第${val}页`);
      this.currentPage = val;
      this.loadAttrtempList();
    },
  },

  mounted() {
    // 加载所有属性模板列表
    this.loadAttrtempList();
  },
};
</script>

